//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ccp.dev.core.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URL;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.FileUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.TimeUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.DocumentResult;
import org.dom4j.io.DocumentSource;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.SAXValidator;
import org.dom4j.io.XMLWriter;
import org.dom4j.util.XMLErrorHandler;
import org.xml.sax.SAXException;

public class Dom4jUtil {
    private static final Log a = LogFactory.getLog(Dom4jUtil.class);

    public Dom4jUtil() {
    }

    public static Document loadXml(String s) {
        Document document = null;

        try {
            document = DocumentHelper.parseText(s);
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return document;
    }

    public static Document load(String filename, String encode) {
        Document document = null;

        try {
            SAXReader saxReader = new SAXReader();
            saxReader.setEncoding(encode);
            document = saxReader.read(new File(filename));
        } catch (Exception var4) {
        }

        return document;
    }

    public static Document loadXml(String xml, String encode) throws UnsupportedEncodingException {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes(encode));
        return loadXml((InputStream)inputStream, encode);
    }

    public static Document loadXml(InputStream is) {
        return loadXml(is, "utf-8");
    }

    public static Document loadXml(InputStream is, String charset) {
        Document document = null;

        try {
            SAXReader reader = new SAXReader();
            reader.setEncoding(charset);
            document = reader.read(is);
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        return document;
    }

    public static void write(Document document, String fileName) throws IOException {
        String xml = document.asXML();
        FileUtil.writeFile(fileName, xml);
    }

    public static void write(String str, String fileName) throws IOException, DocumentException {
        Document document = DocumentHelper.parseText(str);
        write(document, fileName);
    }

    public Document load(URL url) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(url);
        return document;
    }

    public static Document load(String filename) {
        Document document = null;

        try {
            SAXReader reader = new SAXReader();
            document = reader.read(new File(filename));
            document.normalize();
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return document;
    }

    public static String transFormXsl(String xml, String xsl, Map<String, String> map) throws Exception {
        StringReader xmlReader = new StringReader(xml);
        StringReader xslReader = new StringReader(xsl);
        System.setProperty("javax.xml.transform.TransformerFactory", "org.apache.xalan.processor.TransformerFactoryImpl");
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(xslReader));
        if (map != null) {
            Iterator it = map.entrySet().iterator();

            while(it.hasNext()) {
                Entry<String, String> obj = (Entry)it.next();
                transformer.setParameter((String)obj.getKey(), obj.getValue());
            }
        }

        StreamSource xmlSource = new StreamSource(xmlReader);
        StringWriter writer = new StringWriter();
        Result result = new StreamResult(writer);
        transformer.transform(xmlSource, result);
        return writer.toString();
    }

    public static String transXmlByXslt(String xml, String xslPath, Map<String, String> map) throws Exception {
        Document document = loadXml(xml);
        document.setXMLEncoding("UTF-8");
        Document result = styleDocument(document, xslPath, map);
        return docToString(result);
    }

    public static String transFileXmlByXslt(String xmlPath, String xslPath, Map<String, String> map) throws Exception {
        Document document = load(xmlPath);
        document.setXMLEncoding("UTF-8");
        Document result = styleDocument(document, xslPath, map);
        return docToString(result);
    }

    public static String docToString(Document document) {
        String s = "";

        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            OutputFormat format = new OutputFormat("  ", true, "UTF-8");
            XMLWriter writer = new XMLWriter(out, format);
            writer.write(document);
            s = out.toString("UTF-8");
        } catch (Exception var5) {
            a.error("docToString error:" + var5.getMessage());
        }

        return s;
    }

    public static String docToPrettyString(Document document) {
        return docToPrettyString(document, true);
    }

    public static String docToPrettyString(Document document, boolean removeHead) {
        String result = "";

        try {
            Writer writer = new StringWriter();
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setSuppressDeclaration(removeHead);
            XMLWriter xmlWriter = new XMLWriter(writer, format);
            xmlWriter.write(document);
            result = writer.toString();
        } catch (Exception var6) {
            a.error("docToString error:" + var6.getMessage());
        }

        return result;
    }

    public static Document styleDocument(Document document, String stylesheet, Map<String, String> map) throws Exception {
        System.setProperty("javax.xml.transform.TransformerFactory", "org.apache.xalan.processor.TransformerFactoryImpl");
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));
        if (map != null) {
            Iterator it = map.entrySet().iterator();

            while(it.hasNext()) {
                Entry<String, String> obj = (Entry)it.next();
                transformer.setParameter((String)obj.getKey(), obj.getValue());
            }
        }

        DocumentSource source = new DocumentSource(document);
        DocumentResult result = new DocumentResult();
        transformer.transform(source, result);
        Document transformedDoc = result.getDocument();
        return transformedDoc;
    }

    public static String validXmlBySchema(String xml, String schema) {
        String result = "";

        try {
            XMLErrorHandler errorHandler = new XMLErrorHandler();
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setValidating(true);
            factory.setNamespaceAware(true);
            SAXParser parser = factory.newSAXParser();
            SAXReader xmlReader = new SAXReader();
            Document xmlDocument = xmlReader.read(new File(xml));
            parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
            parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", "file:" + schema);
            SAXValidator validator = new SAXValidator(parser.getXMLReader());
            validator.setErrorHandler(errorHandler);
            validator.validate(xmlDocument);
            new XMLWriter(OutputFormat.createPrettyPrint());
            if (errorHandler.getErrors().hasContent()) {
                result = "<result success='0'>XML文件通过XSD文件校验失败,请检查xml是否符合指定格式!</result>";
            } else {
                result = "<result success='1'>XML文件通过XSD文件校验成功!</result>";
            }
        } catch (Exception var10) {
            result = "<result success='0'>XML文件通过XSD文件校验失败:" + var10.getMessage() + "</result>";
        }

        return result;
    }

    public static boolean validByXsd(String xsdPath, InputStream xmlData) {
        SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        File schemaFile = new File(xsdPath);
        Schema schema = null;

        try {
            schema = schemaFactory.newSchema(schemaFile);
        } catch (SAXException var9) {
            var9.printStackTrace();
        }

        Validator validator = schema.newValidator();
        StreamSource source = new StreamSource(xmlData);

        try {
            validator.validate(source);
            return true;
        } catch (Exception var8) {
            a.info(var8.getMessage());
            return false;
        }
    }

    public static String getString(Element element, String attrName) {
        return getString(element, attrName, false);
    }

    public static String getString(Element element, String attrName, Boolean fuzzy) {
        if (BeanUtils.isEmpty(element)) {
            return null;
        } else {
            String val = element.attributeValue(attrName);
            if (StringUtil.isEmpty(val)) {
                return null;
            } else {
                if (fuzzy) {
                    val = "%" + val + "%";
                }

                return val;
            }
        }
    }

    public static void addAttribute(Element element, String attrName, Object val) {
        addAttribute(element, attrName, val, "yyyy-MM-dd HH:mm:ss");
    }

    public static void addAttribute(Element element, String attrName, Object val, String format) {
        if (!BeanUtils.isEmpty(val)) {
            if (val instanceof Date) {
                String dateStr = TimeUtil.getDateTimeString((Date)val, format);
                element.addAttribute(attrName, dateStr);
            } else {
                element.addAttribute(attrName, val.toString());
            }

        }
    }
}

